package com.yanqu.road.dao.impl.activity.patronsfund;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundBuyData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PatronsFundDaoImpl extends TempDataObjectDao<PatronsFundBuyData> {

    public static String selectSql = "select * from t_u_patrons_fund_buy_data where userId=?";
    public Map<Integer, Map<Integer, PatronsFundBuyData>> getPatronsFundBuyData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, PatronsFundBuyData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                PatronsFundBuyData data = parse(rs);
                Map<Integer, PatronsFundBuyData> mallMap = map.get(data.getPatronsId());
                if (mallMap == null) {
                    mallMap = new HashMap<>();
                    mallMap.put(data.getMallRechargeId(), data);
                    map.put(data.getPatronsId(), mallMap);
                } else {
                    mallMap.put(data.getMallRechargeId(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertSql = "insert into t_u_patrons_fund_buy_data(" +
            "userId,patronsId,mallRechargeId,createTime) " +
            " values (?,?,?,?)";
    @Override
    protected boolean addData(PatronsFundBuyData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallRechargeId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_patrons_fund_buy_data " +
            "set patronsId = ?,mallRechargeId = ? " +
            " where userId = ? ";
    @Override
    protected boolean updateData(PatronsFundBuyData data) {
//        data.setBeginWriteTime();
//        getDb().setConnName(DbNameStrategy.getUserDb());
//        int i = 1;
//        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
//        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
//        para.put(i++, new DbParameter(Types.INTEGER, data.getMallRechargeId()));
//        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
//        if (getDb().executeNoneQuery(updateSql, para) > 0) {
//            return true;
//        }
        return true;
    }

    @Override
    protected PatronsFundBuyData parseData(ResultSet rs) throws SQLException {
        PatronsFundBuyData patronsFundBuyData = new PatronsFundBuyData();
        patronsFundBuyData.setUserId(rs.getLong("userId"));
        patronsFundBuyData.setPatronsId(rs.getInt("patronsId"));
        patronsFundBuyData.setMallRechargeId(rs.getInt("mallRechargeId"));
        patronsFundBuyData.setCreateTime(rs.getLong("createTime"));
        return patronsFundBuyData;
    }
}
